package cn.fbs.util;


import java.util.Base64;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES工具类，密钥必须是16位字符串
 */
public class AESUtils {
    /**偏移量,必须是16位字符串*/
    private static final String IV_STRING = "F431E6FF9051DA07";


    /**
     * 产生随机密钥(这里产生密钥必须是16位)
     */
    public static String generateKey() {
        String key = UUID.randomUUID().toString();
        key = key.replace("-", "").substring(0, 16);// 替换掉-号
        return key;
    }

    /**
     * 加密
     * @param key
     * @param content
     * @return
     */
    public static String encryptData(String key, String content) {
        byte[] encryptedBytes = new byte[0];
        try {
            byte[] byteContent = content.getBytes("UTF-8");
            // 注意，为了能与 iOS 统一
            // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = key.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            // 指定加密的算法、工作模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            encryptedBytes = cipher.doFinal(byteContent);
            // 同样对加密后数据进行 base64 编码
//            return Base64Utils.encode(encryptedBytes);
            Base64.Encoder encoder = Base64.getEncoder();
            String encodeToString = encoder.encodeToString(encryptedBytes);
            System.out.println(encodeToString);
            return encodeToString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encryptData( String content) {
        byte[] encryptedBytes = new byte[0];
        try {
            byte[] byteContent = content.getBytes("UTF-8");
            // 注意，为了能与 iOS 统一
            // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = IV_STRING.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            // 指定加密的算法、工作模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            encryptedBytes = cipher.doFinal(byteContent);
            // 同样对加密后数据进行 base64 编码
//            return Base64Utils.encode(encryptedBytes);
            Base64.Encoder encoder = Base64.getEncoder();
            String encodeToString = encoder.encodeToString(encryptedBytes);
            System.out.println(encodeToString);
            return encodeToString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 解密
     * @param key
     * @param content
     * @return
     */
    public static String decryptData(String key, String content) {
        try {
            // base64 解码
//            byte[] encryptedBytes = Base64Utils.decode(content);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] encryptedBytes = decoder.decode(content);
            byte[] enCodeFormat = key.getBytes();
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptData( String content) {
        try {
            // base64 解码
//            byte[] encryptedBytes = Base64Utils.decode(content);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] encryptedBytes = decoder.decode(content);
            byte[] enCodeFormat = IV_STRING.getBytes();
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
////        String plainText = AESUtils.decryptData("F431E6FF9051DA07", "q8jHYk6LSbwC2K4zmr/wRZo8mlH0VdMzPEcAzQadTCpSrPQ/ZnTmuIvQxiLOnUXu");
////        System.out.println("aes加密后: " + plainText);
//        String s = AESUtils.encryptData(IV_STRING, "test");
//        System.out.println(s);
//        String s1 = AESUtils.decryptData(IV_STRING, s);
//        System.out.println(s1);
//
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("name","test");
//        jsonObject.put("password","test");
//        String s2 = jsonObject.toJSONString();
//        String x = AESUtils.encryptData(IV_STRING, s2);
//        System.out.println(x);
//        String x1 = AESUtils.decryptData(IV_STRING, x);
//        System.out.println(x1);
//        String s3 = AESUtils.decryptData("OG4ls2N/zZItANCeEthCnRAY4acpbtf0UOciQXr0spEUUJPJ2OpRdDXMNZVnzmg+");
//        System.out.println(s3);
//        System.out.println("ll");
//        System.out.println(encryptData("k"));
//
//    }

    public static void main(String[] args) {
        String qqq = AESUtils.encryptData("admin");
        System.out.println(qqq);
        System.out.println(AESUtils.decryptData("SA1YTgKCp4y7e5/X4NqbeQ=="));
    }

}